package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import XMLEntities.PlanInMealListXM;
import XMLUtilities.JAXBUtilities;

import DAO.DishDAO;
import DAO.PlanDAO;
import DTO.DishDTO;
import DTO.PlanDTO;

import constants.StringConstants;

/**
 * Servlet implementation class PlanServlet
 */
public class PlanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	PlanDAO planDAO = new PlanDAO();
	DishDAO dishDAO = new DishDAO();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PlanServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession().getAttribute("ADMIN_USERNAME") != null) {
			String action = request.getParameter("action");
			if (action.equals(StringConstants.LOAD_MENU_MEALS)) {
				int menuId = Integer.parseInt(request.getParameter("menuId"));
				int numberOfDays = Integer.parseInt(request
						.getParameter("numberOfDays"));
				int numberMealsPerDay = Integer.parseInt(request
						.getParameter("numberMealsPerDay"));

				request.setAttribute("MENUID", menuId);
				request.setAttribute("NUMBEROFDAYS", numberOfDays);
				request.setAttribute("NUMBERMEALSPERDAY", numberMealsPerDay);

				RequestDispatcher rd = request
						.getRequestDispatcher("admin/menu-plan.jsp");
				rd.forward(request, response);
			} else if (action.equals(StringConstants.LOAD_DISHES_IN_MEALS)) {
				int menuId = Integer.parseInt(request.getParameter("menuId"));
				int mealOrder = Integer.parseInt(request
						.getParameter("mealOrder"));

				ArrayList<DishDTO> dishList = new ArrayList<DishDTO>();
				dishList = dishDAO.select();

				ArrayList<PlanDTO> planList = new ArrayList<PlanDTO>();
				ArrayList<DishDTO> selectedDishList = new ArrayList<DishDTO>();
				planList = planDAO.getByMenuMeal(menuId, mealOrder);
				for (PlanDTO planDTO : planList) {
					DishDTO dish = planDTO.getDish();
					dishDAO.get(dish);
					selectedDishList.add(dish);
				}

				request.setAttribute("MENUID", menuId);
				request.setAttribute("MEALORDER", mealOrder);
				request.setAttribute("DISHLIST", dishList);
				request.setAttribute("SELECTEDDISHES", selectedDishList);

				RequestDispatcher rd = request
						.getRequestDispatcher("admin/meal-pick-dish.jsp");
				rd.forward(request, response);
			}
		} else {
			RequestDispatcher rd = request
					.getRequestDispatcher("admin/admin-login.html");
			rd.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession().getAttribute("ADMIN_USERNAME") != null) {
			String action = request.getParameter("action");
			if (action.equals("load-meals-in-days")) {
				int menuId = Integer.parseInt(request.getParameter("menuId"));
				int fromDay = Integer.parseInt(request.getParameter("fromDay"));
				int toDay = Integer.parseInt(request.getParameter("toDay"));

				ArrayList<PlanDTO> list = new ArrayList<PlanDTO>();
				list = planDAO.getByDay(menuId, fromDay, toDay);

				PlanInMealListXM planInMealListXM = new PlanInMealListXM();
				planInMealListXM = planDAO.convertPlansToPlanInMeals(list);

				JAXBUtilities.marshall_toOutputStream(planInMealListXM,
						response.getOutputStream());
			} else if (action.equals("edit-dish-in-meal")) {
				PrintWriter out = response.getWriter();
				int menuId = Integer.parseInt(request.getParameter("menuId"));
				int mealOrder = Integer.parseInt(request
						.getParameter("mealOrder"));
				String selectedDish_str = request
						.getParameter("selectedDishes");
				String[] selectedDish_ids = selectedDish_str.split(",");

				try {
					planDAO.deleteByMeal(menuId, mealOrder);
					for (String dish_id : selectedDish_ids) {
						int dishId = Integer.parseInt(dish_id);
						PlanDTO planDTO = new PlanDTO();
						planDTO.getMenu().setId(menuId);
						planDTO.setMealOrder(mealOrder);
						planDTO.getDish().setId(dishId);
						planDAO.add(planDTO);
					}
				} catch (Exception e) {
					out.print("fail");
					return;
				}
				out.print("success");
			}
		} else {
			RequestDispatcher rd = request
					.getRequestDispatcher("admin/admin-login.html");
			rd.forward(request, response);
		}
	}
}
